/**
    ATClab - scriptable Air Traffic Control simulations.
    Copyright (C) 2010 openatclab@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/


#include "experiment.h"
#include "translator.h"


#ifdef _WIN32
	#include <assert.h>
#endif

using namespace pact;

////////////////////////////////////////////////////////////////////////////////////////////////////
//
//

/**
 * Default Constructor
 */
Experiment::Experiment()
	  : xml::Document()
	  , _globalState()
	  , _presentation()
	  , _translator(0)
{}

/**
 * Destructor
 */
Experiment::~Experiment() {
	delete _translator;
}

//--------------------------------------------------------------------------------------------------
// public virtual
//

/**
 * Create child document node
 * The actual creation is delegated to Document parent class. If the new node
 * is a child of a 'phase' node (ie it's a task) then it is added to the 
 * presentaion queue.
 */
void Experiment::createChild( const std::string& type ) {
	Document::createChild(type); // sets _current to new node

	if ( _current == _root ) return;

	if      ( "phase" == _current->parent->type )  { _presentation.push_back( _current ); }
	else if ( "global" == _current->parent->type ) { _globalState.push_back( _current ); }
}

/**
 *
 */
bool Experiment::validate()
{

	if ( Document::validate() ) {

		std::string units = getStringContent(
			_root,
			"config units input"
		);

		if("+DDDMMSS" == units){
			_translator = new pact::AeroNauticTranslator;

		}else if("NM-FT" == units){
			_translator = new pact::SimpleAeroNauticTranslator;

		}else if("STANDARD" == units){
			_translator = new pact::MetricTranslator;

		}else{
			throw pact::experiment_error(
				"illegal unit schema <" + units + ">"
			);
		}
		return true;
	}

	return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
